package com.csy.common.xss;

import com.csy.common.exception.RRException;
import org.apache.commons.lang3.StringUtils;

/**
 * 类名:sql过滤
 * Created by CSY on 2018/9/17.
 */
public class SQLFilter {
    /**
     * sql注入过滤
     * @param str 待过滤字符
     */
    public static String sqlInject(String str){
        if(StringUtils.isBlank(str)){
            return null;
        }
        //去掉'|"|;|\字符
        str = StringUtils.replace(str,"'","");
        str = StringUtils.replace(str,"\"","");
        str = StringUtils.replace(str,";","");
        str = StringUtils.replace(str,"\\","");
        //转换为小写
        str = str.toLowerCase();
        //非法字符
        String[] keywords = {"master", "truncate", "insert", "select", "delete", "update", "declare", "alter", "drop"};
        //判断是否含有非法字符
        for(String keyword : keywords){
            if(str.indexOf(keyword) != -1){
                throw new RRException("包含非法字符");
            }
        }
        return str;
    }
}
